package com.rjpacket.supernews.app;

import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import com.rjpacket.supernews.app.models.MyImages;
import com.rjpacket.supernews.app.models.NewsDetail;

import java.util.List;

/**
 * 娱乐新闻的详情Activity
 */
public class FunnyNewsDetailActivity extends BaseActivity {

    private NewsDetail detail;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);

        Intent intent = getIntent();
        detail = (NewsDetail) intent.getSerializableExtra("news");

        title = (TextView) findViewById(R.id.actionbar_title);

        String original_url = detail.getOriginal_url();

        if (original_url != null) {
            title.setText(original_url);
        }

        TextView title = (TextView) findViewById(R.id.detail_title);
        TextView source = (TextView) findViewById(R.id.detail_source);
        TextView time = (TextView) findViewById(R.id.detail_time);

        title.setText(detail.getNews_title());
        source.setText(detail.getNews_source());
        time.setText(detail.getNews_showTime());

        WebView webView = (WebView) findViewById(R.id.webview_news_detail);

        String contents = detail.getNews_content();

        List<MyImages> images = detail.getImages();
        for (MyImages image : images) {
            String imgUrl = "<img src=" + detail.getBase_image_url() + image.getUrl_big() + " width=" + image.getWidth() + " height=" + image.getHeight() + ">";
            contents = contents.replace("news_content_image", imgUrl);
        }

        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.loadDataWithBaseURL("about:blank", contents, "text/html", "utf-8", null);
    }

}
